package com.zhongkai.service.ticket;

import java.util.Iterator;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zhongkai.json.JSONArray;
import com.zhongkai.json.JSONException;
import com.zhongkai.json.JSONObject;
import com.zhongkai.model.declare.TSbCcssbxx;
import com.zhongkai.model.declare.TSbCcsspxx;
import com.zhongkai.model.ticket.TPzJc;
import com.zhongkai.service.BaseService;
@Service
@Transactional
public class Atm_reprintService extends BaseService {
	public List searchcomb(Integer ccdjh) throws Exception
	{
		List ls=select("from TSbCcsspxx pxx,TSbCcssbxx bxx where pxx.spXh=bxx.spXh and bxx.ccdjh=? and bxx.sbfsDm='02' and pxx.sphm='' order by pxx.spXh,bxx.ssqqRq,bxx.znjBj",ccdjh);
		return ls;
	}
	
	public JSONArray CombinateTicket(List tpjclist,int length) throws JSONException
	{
		int ticketamount=length;
		JSONArray tmparray=new JSONArray();
		JSONObject tmpticket;
		Integer start,end;
		String tmpsphmq;
		for(Iterator tpjc=tpjclist.iterator();tpjc.hasNext();)
		{
			TPzJc tpzjc=(TPzJc) tpjc.next();
			start=Integer.parseInt(tpzjc.getSphmZ());
			end=Integer.parseInt(tpzjc.getSphmQ());
			for(int i=0;i<=(start-end);i++)
			{
				
				Integer ph=end+i;
				tmpsphmq=ph.toString();
				tmpticket=new JSONObject(tpzjc);
				int max=tpzjc.getSphmQ().length()-tmpsphmq.length();
				for(int j=0;j<max;j++)
					tmpsphmq="0"+tmpsphmq;
				tmpticket.put("sphmQ", tmpsphmq);
				tmparray.put(tmpticket);
				ticketamount--;
				if(ticketamount==0) break;
			}
			System.out.println(tmparray.toString());
			if(ticketamount==0)
				break;
		}
		return tmparray;
	}
	
	public JSONArray CombinateTax(List taxList) throws JSONException
	{	
		int pzxh=-1;
		JSONObject js=new JSONObject();
		JSONArray totaljs=new JSONArray();
		double totaltax=0;

		for(Iterator taxiter=taxList.iterator();taxiter.hasNext();)
		{
			Object[] nsxx=(Object[]) taxiter.next();
			TSbCcsspxx pxx=(TSbCcsspxx)nsxx[0];
			TSbCcssbxx bxx=(TSbCcssbxx)nsxx[1];
			
			if(pxx.getSpXh()!=pzxh)
			{
				if (js.isNull("pzxx") && totaljs.isNull(0))
				{}
				else
				{
					totaljs.put(js.put("kpJe", totaltax));
					totaltax=0;
				}
				js=new JSONObject();
				js.append("pzxx", new JSONObject(pxx));
				js.append("bzxx", new JSONObject(bxx));
				totaltax+=bxx.getYbtJe();
				pzxh=pxx.getSpXh();
			}
			else
			{
				js.append("bzxx", new JSONObject(bxx));
				totaltax+=bxx.getYbtJe();
				pzxh=pxx.getSpXh();
			}
		}
		totaljs.put(js.put("kpJe", totaltax));
		return totaljs;
	}

}
